/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.huawei.locationsample6;

import android.content.Context;
import android.content.res.AssetManager;

import com.huawei.logger.LocationLog;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class JsonDataUtil {
    public static final String TAG = "JsonDataUtil";

    public static String getJson(Context context, String fileName, boolean isBr) {
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bf = null;
        try {
            AssetManager assetManager = context.getAssets();
            bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName), Charset.forName("UTF-8")));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
                if (isBr) {
                    stringBuilder.append("\n");
                }
            }
        } catch (IOException e) {
            LocationLog.i(TAG, "IOException" + e.getClass().getSimpleName());
        } finally {
            try {
                if (bf != null) {
                    bf.close();
                }
            } catch (IOException e) {
                LocationLog.i(TAG, "IOException" + e.getClass().getSimpleName());
            }
        }
        return stringBuilder.toString();
    }

}
